<?php

namespace app\api\controller\User;

use think\Controller;
use think\Request;

class Address extends Controller
{
    public function __construct()
    {
        parent::__construct();

        $this->AddressModel = model('User.Address');
        $this->UserModel = model('User.User');
    }
    public function index()
    {
        // 接收用户id
        $userid = $this->request->param('userid');

        // $addressList = $this->AddressModel->where(['userid' => $userid])->select();
        $addressList = $this->AddressModel->with(['provinces', 'citys', 'districts'])->where(['userid' => $userid])->select();

        // if (!$addressList) {
        //     $this->error('暂无收货地址');
        // }

        $this->success('查询成功', null, $addressList);
    }
    public function select()
    {
        $params = $this->request->param();
        $user = $this->AddressModel->where('userid', $params['userid'])->find();

        if (!$user) {
            $this->error('您不能修改他人的地址');
        }
        $address = $this->AddressModel->where('id', $params['id'])->find();
        if (!$address) {
            $this->error('该收货地址不存在');
        }

        //修改其他的默认地址为零
        $status = $this->AddressModel->where(['userid' => $params['userid']])->update(['status' => 0]);
        $data = [
            'id' => $params['id'],
            'status' => 1
        ];
        $result = $this->AddressModel->isUpdate(true)->save($data);

        if ($result === FALSE || $status === FALSE) {
            $this->error('修改默认收货地址失败');
        } else {
            $this->success('修改默认收货地址成功', null, $data['id']);
        }
    }

    //添加地址
    public function add()
    {
        $params = $this->request->param();
        $data = [
            'consignee' => $params['consignee'],
            'mobile' => $params['mobile'],
            'address' => $params['address'],
            'userid' => $params['userid'],
        ];

        //设置默认值
        if (isset($params['status']) && $params['status'] == 1) {
            $this->AddressModel->where(['userid' => $params['userid']])->update(['status' => 0]);
            $data['status'] = $params['status'];
        }

        //地区的修改
        if (!empty($params['code'])) {
            $parentpath = model('Region')->where(['code' => $params['code']])->value('parentpath');

            if (!$parentpath) {
                $this->error('所选地区不存在');
            }

            // [$province, $city, $district] = explode(',', $parentpath);
            $array = explode(',', $parentpath);
            $province = $array[0];
            $city = $array[1];
            $district = $array[2];

            if ($province) {
                $data['province'] = $province;
            }

            if ($city) {
                $data['city'] = $city;
            }

            if ($district) {
                $data['district'] = $district;
            }
        }

        $result = $this->AddressModel->validate('common/User/Address')->save($data);

        if ($result === FALSE) {
            $this->error('新增收货地址失败');
        } else {
            $this->success('新增收货地址成功', '/user/address/index');
        }
    }

    //查询地址
    public function info()
    {
        $addrid = $this->request->param('addrid');

        $address = $this->AddressModel->with(['provinces', 'citys', 'districts'])->find($addrid);

        if (!$address) {
            $this->error('该收货地址不存在');
        }

        $this->success('查询成功', null, $address);
    }

    //编辑数据保存
    public function editSave()
    {
        $params = $this->request->param();

        $address = $this->AddressModel->find($params['id']);
        if (!$address) {
            $this->error('该收货地址不存在');
        }

        $data = [
            'id' => $params['id'],
            'consignee' => $params['name'],
            'mobile' => $params['tel'],
            'address' => $params['addressDetail'],
            'userid' => $address['userid']
        ];
        $status = null;
        if (isset($params['isDefault'])) {
            $status = $params['isDefault'] ? 1 : 0;
        }

        //设置默认值
        if ($status == 1) {
            $this->AddressModel->where(['userid' => $address['userid']])->update(['status' => 0]);
            $data['status'] = $status;
        } else {
            $data['status'] = 0;
        }

        //地区的修改
        if (!empty($params['areaCode'])) {
            $parentpath = model('Region')->where(['code' => $params['areaCode']])->value('parentpath');

            if (!$parentpath) {
                $this->error('所选地区不存在');
            }

            // [$province, $city, $district] = explode(',', $parentpath);
            $array = explode(',', $parentpath);
            $province = $array[0];
            $city = $array[1];
            $district = $array[2];

            if ($province) {
                $data['province'] = $province;
            }

            if ($city) {
                $data['city'] = $city;
            }

            if ($district) {
                $data['district'] = $district;
            }
        }

        $result = $this->AddressModel->isUpdate(true)->save($data);
        if ($result === FALSE) {
            $this->error('更新收货地址失败');
        } else {
            $this->success('更新收货地址成功', '/user/address/index');
        }
    }

    //收货地址删除
    public function editDel()
    {
        $addrid = $this->request->param('addrid');
        $address = $this->AddressModel->find($addrid);
        if (!$address) {
            $this->error('该收货地址不存在');
        }

        $result = $this->AddressModel->where('id', $addrid)->delete();
        if ($result === FALSE) {
            $this->error('删除收货地址失败');
        } else {
            $this->success('删除收货地址成功', '/user/address/index');
        }
    }

    //商品订单
    public function order()
    {
        $userid = $this->request->param('userid');

        $address = model('User.Address')->where(['userid' => $userid, 'status' => 1])->find();

        if (!$address) {
            $this->error('暂无默认收货地址');
        }

        $this->success('查询成功', null, $address);
    }
}
